package deviceCombo

import (
	"context"

	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/ent"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/svc"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/dberrorhandler"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/entx"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/types/pancakebot"
	"github.com/suyuan32/simple-admin-common/i18n"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateDeviceComboLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateDeviceComboLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDeviceComboLogic {
	return &CreateDeviceComboLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateDeviceCombo DeviceCombo management
func (l *CreateDeviceComboLogic) CreateDeviceCombo(in *pancakebot.DeviceComboInfo) (*pancakebot.BaseIDResp, error) {

	err := entx.WithTx(l.ctx, l.svcCtx.DB, func(tx *ent.Tx) error {
		err := tx.Device.UpdateOneID(*in.DeviceId).ClearCombos().Exec(l.ctx)
		if err != nil {
			return err
		}
		return tx.Device.UpdateOneID(*in.DeviceId).AddComboIDs(in.ComboIds...).Exec(l.ctx)
	})
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}
	return &pancakebot.BaseIDResp{Id: 0, Msg: i18n.CreateSuccess}, nil

}
